package node_operator

import (
	srcinfo "ServiceEdge/controllers/src/srcInfo"
	client "ServiceEdge/pkg/client/clientset"
	"context"
	"encoding/json"
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func GetNodeMessage() (string, error) {
	clientset := client.DefaultClient()
	if clientset == nil {
		return "", fmt.Errorf("Expected non-nil clientset, but got nil")
	}
	nodes, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
	if err != nil {
		return "", fmt.Errorf("无法获取节点信息: %v\n", err)
	}
	nodeInfos := []srcinfo.NodeInfo{}
	for _, node := range nodes.Items {
		nodeInfo := srcinfo.NodeInfo{
			Name:        node.Name,
			Addresses:   node.Status.Addresses,
			Conditions:  node.Status.Conditions,
			Labels:      node.Labels,
			Capacity:    node.Status.Capacity,
			Allocatable: node.Status.Allocatable,
		}
		nodeInfos = append(nodeInfos, nodeInfo)
	}
	jsonData, err := json.MarshalIndent(nodeInfos, "", "  ")
	if err != nil {
		return "", fmt.Errorf("无法转换为 JSON 格式: %v\n", err)
	}
	return string(jsonData), nil
}

func DeleteNode(nodeName string) error {
	clientset := client.DefaultClient()
	if clientset == nil {
		return fmt.Errorf("Expected non-nil clientset, but got nil")
	}
	// 获取节点
	_, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("无法获取节点 %s: %v", nodeName, err)
	}

	// 删除节点
	err = clientset.CoreV1().Nodes().Delete(context.Background(), nodeName, metav1.DeleteOptions{})
	if err != nil {
		return fmt.Errorf("无法删除节点 %s: %v", nodeName, err)
	}

	fmt.Printf("节点 %s 已标记为删除\n", nodeName)

	// 检查节点是否真正被删除
	_, err = clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
	if err == nil {
		return fmt.Errorf("节点 %s 仍存在，可能删除失败", nodeName)
	}
	return nil
}

func AddLabelToNode(nodeName, key, value string) error {
	clientset := client.DefaultClient()
	if clientset == nil {
		return fmt.Errorf("Expected non-nil clientset, but got nil")
	}
	node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("无法获取节点 %s: %v", nodeName, err)
	}

	labels := node.Labels
	if labels == nil {
		labels = make(map[string]string)
	}

	labels[key] = value
	node.Labels = labels

	_, err = clientset.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{})
	if err != nil {
		return fmt.Errorf("无法更新节点 %s: %v", nodeName, err)
	}

	return nil
}

func RemoveLabelFromNode(nodeName, key string) error {
	clientset := client.DefaultClient()
	if clientset == nil {
		return fmt.Errorf("Expected non-nil clientset, but got nil")
	}
	node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("无法获取节点 %s: %v", nodeName, err)
	}

	labels := node.Labels
	if labels == nil {
		// 节点没有标签，无需删除
		return nil
	}

	delete(labels, key)
	node.Labels = labels

	_, err = clientset.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{})
	if err != nil {
		return fmt.Errorf("无法更新节点 %s: %v", nodeName, err)
	}

	return nil
}

func UpdateLabelOnNode(nodeName, key, value string) error {
	clientset := client.DefaultClient()
	if clientset == nil {
		return fmt.Errorf("Expected non-nil clientset, but got nil")
	}
	node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("无法获取节点 %s: %v", nodeName, err)
	}

	labels := node.Labels
	if labels == nil {
		labels = make(map[string]string)
	}

	labels[key] = value
	node.Labels = labels

	_, err = clientset.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{})
	if err != nil {
		return fmt.Errorf("无法更新节点 %s: %v", nodeName, err)
	}

	return nil
}
